package com.edp.wms.basedata.controller;

import com.edp.wms.basedata.domain.Unit;
import com.edp.wms.basedata.service.UnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/basedata/unit")
@RequiredArgsConstructor
@Api(tags = "单位管理")
public class UnitController {

    private final UnitService unitService;

    /**
     * 获取所有单位列表
     */
    @GetMapping
    @ApiOperation("获取单位列表")
    public ResponseEntity<List<Unit>> getAllUnits() {
        List<Unit> units = unitService.list();
        return ResponseEntity.ok(units);
    }

    /**
     * 根据ID获取单位详情
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID获取单位详情")
    public ResponseEntity<Unit> getUnitById(@PathVariable Long id) {
        Unit unit = unitService.getById(id);
        if (unit != null) {
            return ResponseEntity.ok(unit);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 创建新单位
     */
    @PostMapping
    @ApiOperation("创建单位")
    public ResponseEntity<Unit> createUnit(@RequestBody Unit unit) {
        // 检查必要字段
        if (unit.getUnitCode() == null || unit.getUnitName() == null) {
            return ResponseEntity.badRequest().build();
        }

        // 设置默认状态
        if (unit.getStatus() == null) {
            unit.setStatus("0");
        }

        boolean saved = unitService.save(unit);
        if (saved) {
            Unit savedUnit = unitService.getById(unit.getId());
            return ResponseEntity.status(HttpStatus.CREATED).body(savedUnit);
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 更新单位信息
     */
    @PutMapping("/{id}")
    @ApiOperation("更新单位信息")
    public ResponseEntity<Unit> updateUnit(@PathVariable Long id, @RequestBody Unit unit) {
        // 检查要更新的单位是否存在
        Unit existingUnit = unitService.getById(id);
        if (existingUnit == null) {
            return ResponseEntity.notFound().build();
        }

        // 设置ID确保更新正确的记录
        unit.setId(id);

        // 执行更新操作
        boolean updated = unitService.updateById(unit);
        if (updated) {
            // 返回更新后的完整对象
            Unit updatedUnit = unitService.getById(id);
            return ResponseEntity.ok(updatedUnit);
        } else {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 删除单位
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除单位")
    public ResponseEntity<Void> deleteUnit(@PathVariable Long id) {
        boolean deleted = unitService.removeById(id);
        if (deleted) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}

